package com.project.service.impl;


import com.project.bean.CutPageBean;
import com.project.bean.RoomBean;
import com.project.bean.StudentBean;
import com.project.mapper.IRoomMapper;
import com.project.mapper.IStudentMapper;
import com.project.service.IRoomService;

import java.util.List;

public class IRoomServiceImpl extends BaseService implements IRoomService {
    @Override
    public void add(RoomBean room) {
        IRoomMapper mapper =  session.getMapper(IRoomMapper.class);
        mapper.addRoom(room);
        this.commitAndClose();
    }

    @Override
    public boolean del(int id) {
        IRoomMapper mapper =  session.getMapper(IRoomMapper.class);
        try {
            mapper.delRoom(id);
        }catch (Exception e){
            return false;
        }
//        mapper.updateStatus(2,"正常");
        this.commitAndClose();
        return true;
    }

    @Override
    public RoomBean findById(int id) {
        IRoomMapper mapper =  session.getMapper(IRoomMapper.class);
        IStudentMapper mapper2 = session.getMapper(IStudentMapper.class);
        RoomBean room =  mapper.findById(id);
        room.setInNum(room.getAllNum()-mapper2.findByRoomId(id));
        return room;
    }

    @Override
    public CutPageBean findByItem(int pageNO,String address, String state, String liveRoom, String roomType) {
        IRoomMapper mapper =  session.getMapper(IRoomMapper.class);
        this.flag=false;
        CutPageBean<RoomBean> cutPageBean = new CutPageBean<RoomBean>();
        cutPageBean.setList(mapper.cutByItem(address,state,liveRoom,roomType,
                (pageNO-1)* CutPageBean.PAGESIZE,CutPageBean.PAGESIZE));
        cutPageBean.setCount(mapper.cuntByCount(address,state,liveRoom,roomType));
        this.commitAndClose();
        return cutPageBean;
    }

    public static void main(String[] args){
        IRoomService service = new IRoomServiceImpl();
//        service.add(new RoomBean("D栋",12,"男生宿舍"));
//        System.out.println(service.findById(3));
//        IRoomMapper mapper =  session.getMapper(IRoomMapper.class);
//        mapper.updateStatus(2,"正常");
//        service.del(5);
        System.out.println(service.findByItem(1,"","",
                "",""));
    }
}
